/**
 * Title: UploadManager.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-9-24 下午3:27:08
 * Version 1.0
 */
package com.luoxudong.phonenumber.service.net.http.filetransfer.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.luoxudong.phonenumber.service.net.http.filetransfer.vo.TransferStatus;

/** 
 * ClassName: UploadManager
 * Description:上传队列管理
 * Create by 罗旭东
 * Date 2013-9-24 下午3:27:08
 */
public class UploadManager {
	/**
	 * 每次开启下载任务个数
	 */
	private static final int OPEN_TASK_NUM_ONCE = 5;
	
	/**
	 * 是否结束线程
	 */
	public static boolean isEndThread = false;
	
	/**
	 * 上传任务队列
	 */
	private static List<FileTransferTask> uploadTaskList = new CopyOnWriteArrayList<FileTransferTask>();
	
	/**
	 * 正在上传的任务
	 */
	private static List<FileTransferTask> uploadingTaskList = new CopyOnWriteArrayList<FileTransferTask>();
	
	/**
	 * 添加下载任务到上传队列
	 * @param fileTransferTask 文件下载任务
	 */
	public static void addUploadTask(FileTransferTask fileTransferTask)
	{
		checkUploadTaskList();
		
		synchronized (uploadTaskList) {
			if (!uploadTaskList.contains(fileTransferTask))
			{
				uploadTaskList.add(fileTransferTask);
			}
			
			//当前没有任务在下载
			if (uploadingTaskList.size() == 0)
			{
				startUploadQueue();
			}
		}
	}
	
	/**
	 * 从上传列表中查找指定上传任务
	 * @param uploadId 上传ID
	 * @return 结果下载任务
	 */
	public static FileTransferTask findUpnloadTask(int uploadId)
	{
		checkUploadTaskList();
		
		synchronized (uploadTaskList) {
			for (FileTransferTask fileTransferTask : uploadTaskList)
			{
				if (fileTransferTask.getTransferId() == uploadId)
				{
					return fileTransferTask;
				}
			}
		}
		
		return null;
	}
	
	/**
	 * 获取正在上传任务的ID
	 * @return
	 */
	public static List<Integer> findAllUploadTaskIds()
	{
		checkUploadTaskList();
		List<Integer> uploadIds = new ArrayList<Integer>();
		
		synchronized (uploadTaskList) {
			for (FileTransferTask fileTransferTask : uploadTaskList)
			{
				uploadIds.add(fileTransferTask.getTransferId());
			}
		}
		
		return uploadIds;
	}
	
	/**
	 * 检测是否正在上传
	 * @param uploadId
	 */
	public static boolean isUploading(int uploadId)
	{
		synchronized (uploadingTaskList) {
			if (uploadingTaskList != null)
			{
				for (FileTransferTask fileTransferTask : uploadingTaskList)
				{
					if (fileTransferTask.getTransferId() == uploadId)
					{
						return true;
					}
				}
			}
			
			return false;
		}
	}
	
	/**
	 * 检测上传任务
	 */
	public static void checkUploadTaskList()
	{
		//列表不存在则初始化
		if (uploadTaskList == null)
		{
			uploadTaskList =new CopyOnWriteArrayList<FileTransferTask>();
		}
		
		if (uploadingTaskList == null)
		{
			uploadingTaskList = new CopyOnWriteArrayList<FileTransferTask>();
		}
	}
	
	/**
	 * 移除指定上传任务
	 * @param uploadId 上传ID
	 * @return
	 */
	public static boolean removeUploadTask(int uploadId)
	{
		if (uploadTaskList == null)
		{
			return false;
		}
		
		if (uploadId == -1)
		{
			return false;
		}
		
		synchronized (uploadTaskList) {
			for (FileTransferTask fileTransferTask : uploadTaskList)
			{
				if (fileTransferTask.getTransferId() == uploadId)
				{
					uploadTaskList.remove(fileTransferTask);
					
					if (uploadingTaskList.contains(fileTransferTask))//当前正在下载
					{
						fileTransferTask.cancel();
						uploadingTaskList.remove(fileTransferTask);
					}
					
					//如果还有未下载的任务则开始下载
					for (FileTransferTask ftt : uploadTaskList)
					{
						if (ftt.getTransferStatus() == TransferStatus.ready.getValue())
						{
							uploadingTaskList.add(ftt);
							startUploadTask(ftt);
						}
						
						if (uploadingTaskList.size() >= OPEN_TASK_NUM_ONCE)
						{
							break;
						}
					}
					
					return true;
				}
			}
			
			return false;
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public static int stopAllUploadTask()
	{
		if (uploadTaskList == null)
		{
			return -1;
		}
		
		synchronized (uploadTaskList) 
		{
			for (FileTransferTask fileTransferTask : uploadTaskList)
			{
				fileTransferTask.cancel();
			}
			
			uploadTaskList.clear();
			uploadingTaskList.clear();
			
			isEndThread = true;
			return 1;
		}
	}
	
	/**
	 * 启动上传队列
	 */
	public static void startUploadQueue()
	{
		for (FileTransferTask fileTransferTask : uploadTaskList)
		{
			//把正在等待下载的任务加入下载队列中
			if (fileTransferTask.getTransferStatus() == TransferStatus.ready.getValue() && uploadingTaskList.size() < OPEN_TASK_NUM_ONCE)
			{
				startUploadTask(fileTransferTask);
			}
			
			if (uploadingTaskList.size() >= OPEN_TASK_NUM_ONCE)
			{
				break;
			}
		}
	}
	
	/**
	 * 启动上传任务
	 * @param fileTransferTask
	 */
	private static void startUploadTask(FileTransferTask fileTransferTask)
	{
		if (!uploadingTaskList.contains(fileTransferTask))
		{
			uploadingTaskList.add(fileTransferTask);
		}
		
		fileTransferTask.setTransferStatus(TransferStatus.working.getValue());
		fileTransferTask.start();
	}
}
